﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.Infrastructure.Annotations;
using System.Data.Entity.ModelConfiguration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EfSandbox.Entities.Final.Eod.Mappings {
    public class QuoteMap : EntityTypeConfiguration<Quote> {
        public QuoteMap() {
            // Table
            ToTable("fin.eod_quotes");

            // Primary Key
            HasKey(q => q.QuoteId);

            // Index
            Property(q => q.SymbolId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_QOT_CD", 1) { IsUnique = true }));
            Property(q => q.TradeDate)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_QOT_CD", 2) { IsUnique = true }));
            Property(q => q.FrequencyId)
                .HasColumnAnnotation(IndexAnnotation.AnnotationName, new IndexAnnotation(new IndexAttribute("IX_QOT_CD", 3) { IsUnique = true }));

            // Attributes
            Property(q => q.QuoteId)
                .HasColumnName("qot_id");

            Property(q => q.SymbolId)
                .IsRequired()
                .HasColumnName("qot_sym_id");

            Property(q => q.TradeDate)
                .IsRequired()
                .HasColumnName("qot_trade_dt");

            Property(q => q.FrequencyId)
                .IsRequired()
                .HasColumnName("qot_cat_id");

            Property(q => q.Open)
                .HasPrecision(18,6)
                .HasColumnName("qot_open");

            Property(q => q.High)
                .HasPrecision(18, 6)
                .HasColumnName("qot_high");

            Property(q => q.Low)
                .HasPrecision(18, 6)
                .HasColumnName("qot_low");

            Property(q => q.Close)
                .HasPrecision(18, 6)
                .HasColumnName("qot_close");

            Property(q => q.Volume)
                .HasColumnName("qot_volume");

            Property(q => q.OpenInterest)
                .IsOptional()
                .HasColumnName("qot_open_interest");

            // Releationship
            // Category (Freuency)
            HasRequired(q => q.Frequency)
                .WithMany()
                .HasForeignKey(q => q.FrequencyId);

            // Symbol
            HasRequired(q => q.Symbol)
                .WithMany()
                .HasForeignKey(q => q.SymbolId);
        }
    }
}
